import 'package:cached_network_image/cached_network_image.dart';
import 'package:chat_shop/common/network/network.dart';
import 'package:flutter/material.dart';
import 'package:chat_shop/common/appbar/appbar.dart';
import 'package:zevent_bus/zevent_bus.dart';

class FriendPage extends StatefulWidget {
  @override
  State<FriendPage> createState() => _FriendPageState();
}

class _FriendPageState extends State<FriendPage> with AutomaticKeepAliveClientMixin {
  List<dynamic> data = [];
  @override
  void initState() {
    super.initState();
    _getData();
  }

  void _getData() {
    Networktool.request("v2/list", method: HTTPMETHOD.GET, success: (sender) {
      print("$sender");
      setState(() {
        data.addAll(sender);
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: CustomAppbar.normal(title: "朋友", leading: const SizedBox()),
      body: ListView.builder(
        itemCount: data.length,
        padding: const EdgeInsets.all(12),
        itemBuilder: (context, index) {
          return _Cell(sender: data[index]);
        },
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}

class _Cell extends StatelessWidget {
  final Map<String, dynamic> sender;
  const _Cell({Key? key, required this.sender}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50.0,
      color: Colors.white,
      child: InkWell(
        onTap: () {
          Navigator.pushNamed(context, "/msg", arguments: {"model": sender});
          EventBus.instance.commit(eventKey: 'chat', arg: sender);
        },
        child: Row(
          children: [
            Container(
              width: 36,
              height: 36,
              margin: const EdgeInsets.only(right: 12),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: CachedNetworkImage(
                  fit: BoxFit.fill,
                  imageUrl: sender['download_url'],
                ),
              ),
            ),
            Expanded(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  new Text(sender['author']),
                  const SizedBox(height: 4),
                  Row(
                    children: [
                      Icon(
                        Icons.phone_android,
                        size: 16,
                        color: Colors.grey,
                      ),
                      Text("${sender['height']}"),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
